#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

class Solution {
public:
    int myAtoi(string str) {

        const char *c = str.c_str();
        const char *p = c;
        int mark = 1;
        while(*c != '\0' && *c == ' ') {
            c++;
        }
        if (*c == '+') {
            c++;
        } else if (*c == '-') {
            c++;
            mark = -1;
        }

        long int x = 0, last = 0;

        int overflow = 0;
        while(*c!='\0' && *c != ' ') {
            if (*c < '0' || *c > '9')
                break;
            last = x;
            x = x * 10 + (*c - '0');
            if (x < last) {
                overflow = 1;
                break;
            }
            c++;
        }

        if (overflow || x < INT_MIN || x > INT_MAX) {
            return mark  == 1? INT_MAX : INT_MIN;
        }

        return x*mark;
    }
};


int main() {

    Solution s;
    cout << s.myAtoi(" -33") << endl;
    cout << s.myAtoi("4193 with words") << endl;
    cout << s.myAtoi("9223372036854775808") << endl;
    cout << s.myAtoi("18446744073709551617") << endl;
}

